package cn.hengzhu.main_manager.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.repository.param.LoginParam;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.UserContext;
import cn.hengzhu.main_manager.utils.ann.SystemControllerLog;

@Controller
@RequestMapping("/main")
public class LoginController {
	
	
	@ResponseBody
	@RequestMapping("/login/index")
	@SystemControllerLog(description="登陆")
	public AjaxResult login(LoginParam param) {
		String password = param.getPassword();
		String username = param.getUsername();
		Subject currentUser = SecurityUtils.getSubject();
//		if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken upt = new UsernamePasswordToken(username, password);
			try {
				currentUser.login(upt);
				ManageUser loginUser = (ManageUser) currentUser.getPrincipal();
				UserContext.setUser(loginUser);
				new AjaxResult();
			} catch (UnknownAccountException e) {
				e.printStackTrace();
				return new AjaxResult("用户名或密码不正确!");//用户名
			} catch (IncorrectCredentialsException e) {
				e.printStackTrace();
				return new AjaxResult("用户名或密码不正确!");
			} catch (LockedAccountException e) {
				e.printStackTrace();
				return new AjaxResult("账号被锁定!");
			} catch (AuthenticationException e) {
				e.printStackTrace();
				return new AjaxResult("用户不存在!");
			}
//		}
		return new AjaxResult();
	}
}
